package login;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author 618325
 */
public class itinerary {
    
//customer id need to pass to get list of booking ids
	public ArrayList getBookingIds() throws ClassNotFoundException, SQLException{
		Connection conn = null;
                ArrayList bookingIdList = new ArrayList();
		
        try 
        {
            Class.forName("com.mysql.jdbc.Driver");
        } 
         catch (ClassNotFoundException ex) {
            System.out.println("FAILED TO LOAD MY SQL DRIVER.");
        }
        
        try
        {
            //setting connection and connection string
		  conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/TravelExperts", "root", "root");
		  PreparedStatement stmt = conn.prepareStatement("SELECT BookingId FROM bookings;");//change query and include customer id
		 
		   ResultSet result = stmt.executeQuery();
            ResultSetMetaData rs= result.getMetaData();
            int numCols = rs.getColumnCount();
           
            if(result != null)
            {
                
                //iterating through data
                 while(result.next())
                {
               
                    for (int i = 1; i <= numCols; i++) 
                    {
                        System.out.println(result.getString(i)+ "\n");
                       
                        bookingIdList.add(result.getString(i));
                    } 
                }
            }

            //closing connection
            conn.close();
	       }
        catch(Exception e)
        {
            //catching exception
            System.out.println("Exception occured. Reason:");
             e.printStackTrace();
        }	
    return bookingIdList;
   }	 
	
        
	public ArrayList getBookingDetails(String bookingId) throws ClassNotFoundException, SQLException{
		Connection conn = null;
                ArrayList bookingIdList = new ArrayList();
		
        try 
        {
            Class.forName("com.mysql.jdbc.Driver");
        } 
         catch (ClassNotFoundException ex) {
            System.out.println("FAILED TO LOAD MY SQL DRIVER.");
        }
        
        try
        {
            //setting connection and connection string
              conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/TravelExperts", "root", "root");
            PreparedStatement stmt = conn.prepareStatement("SELECT * FROM bookings WHERE BookingId=?;");
            stmt.setString(1,bookingId);// parameter position	
            ResultSet result = stmt.executeQuery();
            ResultSetMetaData rs= result.getMetaData();
            int numCols = rs.getColumnCount();
           
            if(result != null)
            {
                
                //iterating through data
                 while(result.next())
                {
               
                    for (int i = 1; i <= numCols; i++) 
                    {
                        System.out.println(result.getString(i)+ "\n");
                       
                        bookingIdList.add(result.getString(i));
                    } 
                }
            }

            //closing connection
            conn.close();
	       }
        catch(Exception e)
        {
            //catching exception
            System.out.println("Exception occured. Reason:");
             e.printStackTrace();
        }	
    return bookingIdList;
   }	 
	
}
